<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 选择城市 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="container" v-if="loadEnd">
		<view class="container-list">
			<view class="list-item" v-for="(item, index) in cityList" :key="index" @click="handleSelect(item)">
				{{ item.name }}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 城市列表
				cityList: [],
				// 省份id
				provinceId: '',
				// 省份名字
				provinceName: '',
			}
		},
		onLoad(option) {
			this.provinceId = option.id
			this.provinceName = option.name
			uni.showLoading({
				title: "加载中"
			})
			this.getCityList(() => {
				this.loadEnd = true
				uni.hideLoading()
			})
		},
		methods: {
			// 获取城市列表
			getCityList(fn) {
				this.$util.request("main.address.city", {
					pid: this.provinceId
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.cityList = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取城市列表 ', error)
				})
			},
			// 选择城市
			handleSelect(item) {
				this.$store.commit('app/setCurrentCity', {
					province: this.provinceName,
					city: item.name,
					latitude: item.lat,
					longitude: item.lng,
				})
				uni.navigateBack({
					delta: 2,
				})
			},
		},
	}
</script>

<style lang="scss">
	.container {
		padding: 32rpx;

		.container-list {
			padding: 16rpx 0;
			border-radius: 16rpx;
			background: #fff;

			.list-item {
				padding: 16rpx 32rpx;
				font-size: 28rpx;
				line-height: 50rpx;
				color: #242629;
			}
		}
	}
</style>